package com.jeff.view.library;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author: duff
 * @date: 2018/12/24
 * @since: 1.0.0
 */
public class LoadingDialog extends Dialog {
    private View mViewRoot;
    private ImageView mLoadingView;
    private TextView mLoadingMessage;
    private ObjectAnimator mRotateAnimator;

    private int mBackgroundResource;
    private int mLoadingImage;
    private CharSequence mLoadingText;
    private @ColorInt
    int mLoadingTextColor;

    public LoadingDialog(@NonNull Context context) {
        this(context, android.R.style.Theme_Translucent_NoTitleBar);
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initDefault();
    }

    public LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initDefault();
    }

    public LoadingDialog(Context context,
                         int backgroundResource,
                         int loadingImageResource,
                         CharSequence loadingText,
                         int loadingTextColor) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        mBackgroundResource = backgroundResource;
        mLoadingImage = loadingImageResource;
        mLoadingText = loadingText;
        mLoadingTextColor = loadingTextColor;
    }

    private void initDefault() {
        // default value
        this.mBackgroundResource = R.drawable.x_loading_dialog_default_backgroud;
        this.mLoadingImage = R.drawable.x_icon_loading;
        this.mLoadingText = getContext().getText(R.string.x_loading_text);
        this.mLoadingTextColor = getContext().getResources().getColor(android.R.color.white);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.x_loading_dialog);

        mViewRoot = findViewById(R.id.root);
        mLoadingView = findViewById(R.id.iv_loading);
        mLoadingMessage = findViewById(R.id.tv_message);

        mViewRoot.setBackgroundResource(mBackgroundResource);
        mLoadingView.setImageResource(mLoadingImage);
        mLoadingMessage.setText(mLoadingText);
        mLoadingMessage.setTextColor(mLoadingTextColor);

        mRotateAnimator = ObjectAnimator.ofFloat(mLoadingView,
                "rotation", 0f, 360.0f);
        mRotateAnimator.setDuration(1500);
        mRotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mRotateAnimator.setRepeatMode(ValueAnimator.RESTART);
        mRotateAnimator.setInterpolator(new LinearInterpolator());
    }

    @Override
    public void show() {
        super.show();
        if (mRotateAnimator != null &&
                !mRotateAnimator.isRunning())
            mRotateAnimator.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mRotateAnimator != null &&
                mRotateAnimator.isRunning())
            mRotateAnimator.end();
    }

    public static class Builder {
        Context context;
        int backgroundResource;
        int loadingImageResource;
        CharSequence loadingText;
        @ColorInt
        int loadingTextColor;

        public Builder(Context context) {
            this.context = context;
            // default value
            this.backgroundResource = R.drawable.x_loading_dialog_default_backgroud;
            this.loadingImageResource = R.drawable.x_icon_loading;
            this.loadingText = context.getText(R.string.x_loading_text);
            this.loadingTextColor = context.getResources().getColor(android.R.color.white);
        }

        public Builder background(@DrawableRes int resId) {
            this.backgroundResource = resId;

            return this;
        }

        public Builder loadingImage(@DrawableRes int resId) {
            this.loadingImageResource = resId;

            return this;
        }

        public Builder loadingText(@StringRes int resId) {
            loadingText(context.getText(resId));

            return this;
        }

        public Builder loadingText(CharSequence message) {
            this.loadingText = message;

            return this;
        }

        public Builder loadingTextColor(@ColorRes int resId) {
            this.loadingTextColor = context.getResources().getColor(resId);

            return this;
        }

        public Builder loadingTextColorInt(@ColorInt int color) {
            this.loadingTextColor = color;

            return this;
        }

        public LoadingDialog build() {
            return new LoadingDialog(context,
                    backgroundResource,
                    loadingImageResource,
                    loadingText,
                    loadingTextColor);
        }

    }

}